package com.bigdatan.b2c.controller;

import constant.SystemCode;
import org.apache.log4j.Logger;
import util.JsonResponse;
import util.SessionUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共controller
 */
public abstract class AbstractController {


    protected JsonResponse<Map<String,Object>> res = new JsonResponse<>();

    /**
     * 日志
     */
    protected Logger logger = Logger.getLogger(this.getClass());

    /**
     * 打印日志
     */
    protected void logInfo(HttpServletRequest request, String msg) {
        logger.info(SessionUtil.getLogHead(request) + msg);
    }

    protected void logError(HttpServletRequest request, String msg, Throwable e) {
        logger.error(SessionUtil.getLogHead(request) + msg, e);
    }

    protected void logError(String msg, Throwable e) {
        logger.error(msg, e);
    }



    protected JsonResponse<Map<String,Object>> successResp(String k,Object v){
        Map<String,Object> map = new HashMap<>();
        map.put(k,v);
        res.setObj(map);
        res.setRes(SystemCode.SUCCESS);
        return res;
    }

    protected JsonResponse<Map<String,Object>> successResp(String k,Object v,String k1,Object v1){
        Map<String,Object> map = new HashMap<>();
        map.put(k,v);
        map.put(k1,v1);
        res.setObj(map);
        res.setRes(SystemCode.SUCCESS);
        return res;
    }
}
